home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 3 #7 & #8 / IMG 29 JulyAug 1995.iso / IMG Jul⁄Aug 95 / IMG Jul_Aug 95.rsrc / TEXT_144.txt < prev    next >
Text File  |  1995-07-29  |  23KB  |  310 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. IMG: Tricks of the Mac Game Programming Gurus
  15.  
  16. Tricks of the Mac Game Programming Gurus is the ultimate resource for beginning to expert game programmers who already have general programming experience.
  17.  
  18. The book contains instruction, tips, and source code from the top names in Mac game development today. The secrets, examples, and code can‚Äôt be found anywhere else! These are the tried-and-true tricks that work behind the scenes in the most popular commercial and shareware Mac games.
  19.  
  20. Throughout the book, you‚Äôll find special interviews with some of the most well-known Mac game programmers. They reveal their secret solutions created while they developed their popular games. The CD-ROM includes various helpful development tools, libraries, utilities as well as all the source code from the book, Metrowerks‚Äô CodeWarrior Lite environment, and Many game demos, plus shareware and commercial games.
  21.  
  22. You can purchase Tricks of the Mac Game Programming Gurus (over 900 pages plus CD-ROM) directly through Inside Mac Games for $50.00 plus shipping. To order, check out the IMG Bookstore folder found on this CD-ROM.
  23.  
  24. Here‚Äôs an excerpt from Tricks of the Mac Game Programming Gurus!
  25.  
  26.                                                                                     Chapter 9: QuickDraw 3D
  27.  
  28. QuickDraw 3D is Apple‚Äôs latest addition to the QuickDraw family of graphics routines. This new suite of Toolbox calls provides even a novice game programmer with the ability to quickly and easily create highly detailed, three-dimensional worlds for Power Macs with only a minimal understanding of 3-D mathematics. Apple has provided a library of powerful 3-D routines numbering in the hundreds, and the Inside Macintosh: QuickDraw 3D  volume is one of the largest volumes in the Inside Macintosh series. But do not let this new library‚Äôs friendly interface fool you, for QuickDraw 3D is very powerful, fairly fast, and produces very high-quality rendered scenes.
  29.  
  30. QuickDraw 3D can render in only two different modes: wireframe and Z-Buffer. However, it does not currently support ‚Äúdepth sort‚Äù rendering, which is perhaps the most common and fastest rendering method used in 3-D video games. You may have heard the depth sort render also referred to as the ‚Äúpainter‚Äôs algorithm.‚Äù To render an image with this method, all of the polygons in a scene are sorted from farthest away to nearest. Then, they are drawn in that order: back to front so that closer polygons are drawn over farther polygons (see figure 9.1). There are two benefits to a depth sort renderer. First, it‚Äôs very fast (more time is sometimes spent doing the 3-D math to rotate, scale, and translate polygons than is spent actually drawing them). Second, it uses far less memory than the Z-Buffer method. Depth sort rendering is used in almost all polygon-based games and is the method of choice in most dedicated game machines such as the 3DO, Sega Saturn, and Sony PlayStation.
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.                                                                                 Figure 9.1 Depth sorting.
  46.  
  47. Even though the depth sort method is the most commonly used method in video games, QuickDraw 3D does not currently use it. The next best thing that it provides is the Z-Buffer renderer. Z-Buffer rendering is much slower than depth sort rendering, and Z-Buffer rendering requires a lot of RAM. The basic principle behind this rendering algorithm is that for each pixel on the screen, there is a value which represents the distance from the camera to the currently drawn pixel. This distance value is called the Z value, hence the ‚ÄúZ-Buffer.‚Äù This way, when a new polygon is being drawn, each pixel‚Äôs Z value is compared with the Z value of the pixel already there. If the new pixel has a smaller Z value (i.e. it‚Äôs closer to the camera) than the old pixel, then the new pixel is drawn over the old one.
  48.  
  49. The reward for the increased complexity and decreased performance of the Z-Buffer algorithm is a much higher-quality rendering, because this algorithm allows for polygons to cleanly intersect each other (something which is almost impossible to do with a depth sort). Figure 9.2 shows the classic example of an impossible situation for a depth sort renderer. In this figure, every polygon overlaps another polygon; therefore, no polygon is in front or in back. Since a depth sort renderer relies entirely on accurate back-to-front sorting of polygons, it will fail in this example. A Z-Buffer renderer, however, does not care about the depth sorting of polygons, thus it can render this case without any problem.
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. Figure 9.2 The classic ‚Äúimpossible case‚Äù scenario for depth sort rendering.
  62.  
  63. The QuickDraw 3D designers have done an excellent job in optimizing their Z-Buffer renderer, so much so that obtaining the frame rates necessary in certain kinds of video games is easily within reach. You probably won‚Äôt be doing any full-screen flight simulators with QuickDraw 3D, but a lot of possibilities still exist. With QuickDraw 3D and a good knowledge of fighting games, you could easily create a 3-D polygon fighting game such as Sega‚Äôs Virtua Fighter. Puzzle games that don‚Äôt require high frame rates could be done incredibly well. Even adventure games could be written to make excellent use of QuickDraw 3D to generate camera fly-throughs of the enemy village, or perhaps your character‚Äôs eye view of the inside of a dragon‚Äôs cave.
  64.  
  65. It should also be pointed out here that QuickDraw 3D is not just a software library. There is also an accelerator card for the PCI-based Power Macintoshes which can boosts performance up to 12 times depending on the implementation of QuickDraw 3D in your game! Even though you would be limiting your game‚Äôs market, you could write a game specifically for use with the accelerator card, in which case you probably could do a full-screen flight simulator. As a matter of fact, you could probably do the best-looking flight simulator the Mac has ever known!
  66.  
  67. Aside from QuickDraw 3D‚Äôs quick rendering speed and quality, it touts many other great features:
  68.  
  69. ‚Ä¢ Built-in Geometries ‚ÄîSupport for boxes, cones, cylinders, disks, ellipses, ellipsoids, general polygons, lines, polylines, meshes, NURB Curves, NURB Patches, points, tori, triangles, and trigrids. However, not all of these are supported in QuickDraw 3D 1.0.
  70.  
  71. ‚Ä¢ Hierarchical Attributes ‚ÄîAttributes may be assigned to groups of objects, individual objects, faces of objects, and vertices of faces. Attributes include color, ambient reflection values, specular colors, and so on.
  72.  
  73. ‚Ä¢ Multiple Drawing Styles ‚ÄîThe ability to adjust the quality of the rendering. This includes techniques such as polygon backface removal, vertex color interpolation, and tessellation of spline-based geometries.
  74.  
  75. ‚Ä¢ Powerful Lighting Features ‚ÄîThe four types of lights include ambient lights, directional lights, point lights, and spot lights. Spot lights can easily be aimed and focused. Light attenuation (‚Äúfall-off‚Äù values) can be individually set for each light.
  76.  
  77. ‚Ä¢ Object Picking ‚ÄîThe ability to provide easy user interface with the mouse. Clicked objects and their associated data can easily be found from mouse and window information.
  78.  
  79. ‚Ä¢ 3D Math Utilities ‚ÄîA large set of 3-D mathematical utilities for performing simple tasks such as setting vectors and points, as well as the more involved tasks such as calculating distances, dot products, and matrix manipulations.
  80.  
  81. Getting Started
  82. The most difficult part of QuickDraw 3D is getting your first application up and running. Unlike plain old QuickDraw, which requires nothing more than a SetPort() and a LineTo() call to see results, QuickDraw 3D requires a lot of setup code. Even to display a line on the screen, you must perform many initialization tasks including setting the camera, creating lights, initializing the draw environment, and so on. But before we get started, it‚Äôs important to have a basic understanding of the QuickDraw 3D Object Hierarchy and a rough idea how attributes work.
  83.  
  84. Objects
  85. Almost every element in QuickDraw 3D is an ‚Äúobject.‚Äù Cameras are objects, geometries are objects, even attributes, shaders, and styles are objects. All of these objects are linked together hierarchically (see figure 9.3). The highest-level object is the group object. A group object (or simply a ‚Äúgroup‚Äù) is simply an object that contains references to more objects, even other group objects. It is a convenient way to collect all of the objects in a scene or part of a scene into one easily manageable entity. If you have 50 missiles flying around your scene, it is much easier to attach all of them to one group called gMyMissleGroupObject and draw that single object than it would be to always draw the 50 missiles one at a time. It‚Äôs also more efficient to pass groups of objects to QuickDraw 3D than to pass lots of individual objects one at a time.
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.                          Figure 9.3 The generic QuickDraw 3D hierarchy.
  114.  
  115. Attributes
  116. The lowest-level object in the QuickDraw 3D hierarchy is the attribute object. An attribute is an object that defines how a geometric object should be drawn in a scene. Attributes include such things as what color the object is, how shiny the object is, and how to apply a texture map to the object. Attributes are always assigned to geometric objects or elements of a geometric object. These elements are usually polygon faces and vertices.
  117.  
  118. Attributes function by a system known as ‚Äúinheritance.‚Äù Inheritance means that attributes of higher-level elements are passed down to lower-level elements of a geometry unless those lower-level elements have overriding attributes of their own. For example, the color attribute of a vertex will override the color attribute of a face, which in turn will override the color attribute of the geometry. If you assign a mesh object a color attribute of green, the geometry will appear blue if the individual faces of the mesh were assigned a color attribute of blue. 
  119.  
  120. QuickDraw 3D is immense[md]this entire book could be dedicated to a discussion of the intricacies of QuickDraw 3D‚Äôs hierarchy and attribute rules, but since that would bore us all to tears, detailed discussions about attributes, objects, and hierarchies will be left up to the massive Inside Macintosh: QuickDraw 3D  volume. Instead, we‚Äôre going to delve right into the code to get us up and running.
  121.  
  122. Initializing QuickDraw 3D
  123. The first thing we must do to get up and running is to initialize QuickDraw 3D. In your application‚Äôs startup routine, you must insert the following code to activate QuickDraw 3D.
  124.  
  125.   /* Initialize QD 3D */
  126.  
  127. TQ3Status    myStatus;
  128.  
  129.  myStatus = Q3Initialize();
  130.  if ( myStatus == kQ3Failure )
  131.   DoFatalAlert("\pQ3Initialize returned failure!");
  132.  
  133. QuickDraw 3D Toolbox routines often return an error status of either kQ3Failure or kQ3Success. Unlike many other Macintosh tool calls, the Boolean values here are reversed. kQ3Success equals 1, and kQ3Failure equals 0; therefore, be careful not to write code like you may have in the past, in which you perform simple error checks like this:
  134.  
  135. OSErr    error;
  136.  
  137.  error = ToolboxCall();
  138.  if (error)
  139.   HandleQ3ror();
  140.  
  141. Since the returned value for an error is no longer 1, this will give you the opposite effect that you want. It is good practice in QuickDraw 3D to always check error conditions by explicitly comparing against kQ3Success or kQ3Failure.
  142.  
  143. Creating a Draw Environment
  144. Once we have initialized QuickDraw 3D, we need to create a draw environment before we can do anything. The draw environment defines all of the basic information that QuickDraw 3D needs to know in order to set up a scene. We do this by executing the following ten steps.
  145.  
  146.  1.    Create a window in which the scene will be drawn.
  147.  2.    Create a new view object.
  148.  3.    Attach a draw context to the view.
  149.  4.    Attach a renderer to the view.
  150.  5.    Attach a camera to the view.
  151.  6.    Attach a light group to the view.
  152.  7.    Create an interpolation style object.
  153.  8.    Create a backfacing style object.
  154.  9.    Create a fill style object.
  155.  10.    Create a shader object.
  156.  
  157. At first glance, this may seem like a lot to do[md]and it is. Luckily for you, however, I have provided all of the code necessary to perform the above ten tasks. The code necessary to set up our drawing environment teaches many of QuickDraw 3D‚Äôs most commonly used principles, so it is a good idea to thoroughly read the following examples and to get a good feel for how and why things work. If it makes sense to you, then you should have no problem with anything else in the QuickDraw 3D world. The first step is trivial: If you‚Äôre ready to tackle QuickDraw 3D, I presume you are familiar with creating Mac windows. The technique is listed in the code and I won‚Äôt repeat it here.
  158.  
  159. Creating the View Object
  160. The following routines initialize a view object and perform steps 2 through 6 as described above.
  161.  
  162. /******************* CREATE MY VIEW *************************/
  163. //
  164. // This creates our view object.
  165. //
  166. // INPUT     :    none
  167. // OUTPUT    :    none
  168. //
  169. // NOTE: gMyViewObject is a global variable of type TQ3ViewObject
  170. //
  171.  
  172. void CreateMyView(void)
  173. {
  174. TQ3Status       myErr;
  175. TQ3DrawContextObject    myDrawContext;
  176. TQ3RendererObject     myRenderer;
  177. TQ3GroupObject      myLights;
  178.  
  179.  
  180.     /* CREATE NEW VIEW OBJECT */
  181.  gMyViewObject = Q3View_New();
  182.  if (gMyViewObject == nil)
  183.   DoFatalAlert("\pQ3View_New failed!");
  184.  
  185.    /* CREATE A DRAW CONTEXT OBJECT */
  186.  myDrawContext = CreateMyDrawContex(gMyWindow);
  187.  if (myDrawContext == nil)
  188.   DoFatalAlert("\pMyNewDrawContext Failed!");
  189.  
  190.    /* ASSIGN DRAW CONTEXT TO THE VIEW */
  191.  myErr= Q3View_SetDrawContext(gMyViewObject, myDrawContext);
  192.  if ( myErr== kQ3Failure )
  193.   DoFatalAlert("\pSetDrawContext Failed!");
  194.  
  195.     /* CREATE RENDERER OBJECT */
  196.  myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  197.  if (myRenderer == nil)
  198.   DoFatalAlert("\pRenderer_NewFromType Failed!");
  199.  
  200.    /* ASSIGN RENDERER TO THE VIEW */
  201.  myErr= Q3View_SetRenderer(gMyViewObject, myRenderer);
  202.  if ( myErr== kQ3Failure )
  203.   DoFatalAlert("\pSetRenderer Failed!");
  204.  
  205.    /* CREATE A CAMERA OBJECT */
  206.  gMyCameraObject = CreateMyCamera(myDrawContext);
  207.  if (gMyCameraObject == nil)
  208.   DoFatalAlert("\pCreateMyCamera Failed!");
  209.  
  210.    /* ASSIGN CAMERA TO THE VIEW */
  211.  myErr= Q3View_SetCamera(gMyViewObject, gMyCameraObject);
  212.  if ( myErr== kQ3Failure )
  213.   DoFatalAlert("\pSetCamera Failed!");
  214.  
  215.    /* CREATE A LIGHT GROUP OBJECT */
  216.  myLights = CreateMyLightGroup();
  217.  if ( myLights == nil )
  218.   DoFatalAlert("\pCreateMyLightGroup Failed!");
  219.   
  220.   /* ASSIGN LIGHT GROUP TO THE VIEW */
  221.  myErr= Q3View_SetLightGroup(gMyViewObject, myLights);
  222.  if ( myErr== kQ3Failure )
  223.   DoFatalAlert("\pSetLightGroup Failed!");
  224. }
  225.  
  226. The routine CreateMyView() above initializes a QuickDraw 3D view object in a very straightforward manner. It creates the new view object simply by calling the Q3View_New() function, and then it proceeds to attach the required objects to the view. The first object attached is the draw context object (see CreateMyDrawContex() coming up). A draw context defines where a scene will be rendered, such as GWorld, screen, and so on.
  227.  
  228. The next object to be assigned to our view is a renderer object. The routine Q3Renderer_NewFromType() takes a predefined renderer type as input. The setting kQ3RendererTypeInteractive tells QuickDraw 3D to use the Z-Buffer renderer and to automatically use hardware acceleration if available.
  229.  
  230. Note
  231. There is an additional call that can be made immediately after creating a renderer which can improve performance with hardware accelerators.
  232.  
  233. Q3InteractiveRenderer_SetDoubleBufferBypass(renderer,kQ3True);
  234.  
  235. This line of code tells QuickDraw 3D not to double buffer if the acceleration hardware is capable of displaying fast enough without it. This can theoretically improve performance up to 10 percent under the right conditions. Even on fast renderers, however, you still may see some flickering due to the elimination of the double buffer.
  236.  
  237. Now that we set up where our scene will appear, we proceed to define our scene‚Äôs camera via CreateMyCamera(). As you will see later, this routine creates a camera object that defines where our ‚Äúeye‚Äù is located in 3-D space and what it is looking at.
  238.  
  239. The final object which needs to be attached to our view is the light group object. The light group object is a group object that contains all of the lights in our scene. Any scene must have at least one light if you want to be able to see anything. A scene rendered with no lights will be black. 
  240.  
  241. Creating the Draw Context
  242. Let‚Äôs take a closer look at some of the routines that were called in the code above. The first routine that we called was CreateMyDrawContex(). Here is the code for CreateMyDrawContex():
  243.  
  244. /**************** CREATE MY DRAW CONTEXT *********************/
  245. //
  246. // Creates a DRAW CONTEXT which will be attached to the fly-through VIEW object.
  247. // The draw context defines how a frame is drawn to a window or off-screen buffer.
  248. //
  249. // INPUT     :    pointer to the window we will be drawing in
  250. // OUTPUT    :    the new draw object
  251. //
  252.  
  253. TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow)
  254. {
  255. TQ3DrawContextObject    myDrawContext;
  256. TQ3DrawContextData     myDrawContextData;
  257. TQ3MacDrawContextData    myMacDrawContextData;
  258. TQ3ColorARGB       clearColor = {1,0,0,0.25};        // dark blue
  259.  
  260.  
  261.    /* FILL IN DRAW CONTEXT DATA */
  262.  myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;    // how to clear
  263.  myDrawContextData.clearImageColor = clearColor;          // color to clear with
  264.  myDrawContextData.pane.min.x = (theWindow->portRect).left;     // set bounds
  265.  myDrawContextData.pane.max.x = (theWindow->portRect).right;
  266.  myDrawContextData.pane.min.y = (theWindow->portRect).top;
  267.  myDrawContextData.pane.max.y = (theWindow->portRect).bottom;
  268.  myDrawContextData.paneState = kQ3True;           // use bounds = yes
  269.  myDrawContextData.maskState = kQ3False;          // no mask
  270.  myDrawContextData.doubleBufferState = kQ3True;       // use double buffering
  271.  
  272.     /* SET MAC-SPECIFIC DATA */
  273.  myMacDrawContextData.drawContextData = myDrawContextData;
  274.  myMacDrawContextData.window = (CWindowPtr)theWindow;     // set window to draw into
  275.  myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  276.  myMacDrawContextData.viewPort = nil;            // (for QuickDraw GX only)
  277.  myMacDrawContextData.grafPort = nil;    
  278.  
  279.   /* CREATE MACINTOSH DRAW CONTEXT BASED ON THAT DATA */
  280.  myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData);
  281.  if (myDrawContext == nil)
  282.   DoFatalAlert("\pQ3MacDrawContext_New failed");
  283.  
  284.  return (myDrawContext);
  285. }
  286.  
  287. This routine creates and returns a draw context object. To create the object, we first set up a TQ3DrawContextData structure and a TQ3MacDrawContextData structure. They will contain all of the information necessary to define our draw context.
  288.  
  289. The first two items initialized are the clearImageMethod field and clearImageColor field. The clearImageColor field defines the RGB color for our scene‚Äôs background. In this example, we have set the clear color to dark blue. RGB colors in QuickDraw 3D are expressed as three floating point numbers between 0.0 and 1.0, where 0.0 is the darkest value and 1.0 is the brightest value for each color component red, green, and blue. Therefore, an RGB value of 1.0, 1.0, 1.0 is white, and the value 0.0, 0.0, 0.0 is black. Solid red would be 1.0, 0.0, 0.0. However, it is important to note that there are two types of color definitions: TQ3ColorRGB and TQ3ColorARGB. The TQ3ColorARGB structure is used in the code sample above. The A stands for ‚ÄúAlpha.‚Äù The alpha value of a TQ3ColorARGB structure determines the amount of transparency for special hardware effects. We won‚Äôt be worrying about alpha values in our discussion, so we‚Äôll just set it to 1.0. The value 1.0 tells QuickDraw 3D that there is no transparency. A value of 0.0 would be maximum transparency.
  290.  
  291. The pane, which is the next field to be initialized, is analogous to the QuickDraw Rect structure because it defines the four edges of a rectangle. QuickDraw 3D uses the pane to determine where in the window we want it to render our scene. In the code above, we are setting the pane to the size of the entire window by getting the window‚Äôs portRect structure and copying it to the draw context‚Äôs pane structure. Suppose we did not want to draw into the entire window‚Äîsuppose we wanted to leave a 20-pixel-wide area on the right side of the window for a game‚Äôs status bar. To do this, simply change the line:
  292.  
  293.  myDrawContextData.pane.max.x = (theWindow->portRect).right;
  294. to:
  295.  myDrawContextData.pane.max.x = (theWindow->portRect).right-20;
  296.  
  297. That will reduce the right side of the pane to leave a 20-pixel-wide gap.
  298.  
  299. There is also a paneState flag which is set. If this flag is set to kQ3False, then the pane min/max settings will be ignored and the entire window will be used by default. Since in our example we are using the whole window, we could have simplified this code by just setting the paneState flag to kQ3False and not even bothered with the pane bounds.
  300.  
  301. The next item in the draw context‚Äôs data structure is the doubleBufferState flag. Double buffering is the process of drawing to one draw buffer while displaying another. We have set the doubleBufferState flag to kQ3True and you will usually want to have it set that way when you are rendering directly to a window. Turning double buffering off will result in a tremendous amount of tearing on the screen as you animate the scene. Power Macs equipped with hardware acceleration for QuickDraw 3D may be able to live without double buffering at the cost of minimal screen tearing.
  302.  
  303. Our draw context data structure now needs to be attached to a TQ3MacDrawContextData structure. The TQ3MacDrawContextData structure defines Macintosh-specific information about the draw context. Because QuickDraw 3D will be available for Microsoft Windows as well as the Macintosh, there are separate data structures that define the draw context for each machine. Luckily, we don‚Äôt care about Windows, so we‚Äôll pretend that QuickDraw 3D exists only for the Macintosh (the way it should be, eh?). Should you want to render to an off-screen buffer and not a window, then you need to use a TQ3PixmapDrawContextData structure instead. You cannot render to an off-screen buffer such as a GWorld by using a TQ3MacDrawContextData structure.
  304.  
  305. The TQ3MacDrawContextData structure‚Äôs window parameter is set to the WindowPtr that we passed into CreateMyDrawContex(). This will tell QuickDraw 3D which window to render into. Make sure that a window exists before doing this. Passing a bogus window will obviously have, oh, shall we say, ‚Äúadverse‚Äù side effects.
  306.  
  307. The next parameter set is the library parameter. For our purposes, this should always be set to kQ3Mac2DLibrary_None. Viola! All the data we need to define a draw context has been set. Once we call Q3MacDrawContext_New(), we have our new draw context object which gets passed back to the calling routine.
  308.  
  309. The only part of this routine that you will probably ever change is the clearImageColor setting, because you will want to experiment with background colors for your scene. All the other settings will most likely remain unchanged.
  310.